package com.anxin.act.task.service;

import com.anxin.act.task.complete.TaskComplete;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.StringUtils;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程特殊节点处理
 */
@Service
public class ActSpecialTaskService {

    @Autowired
    private TaskService taskService;

    @Autowired
    ApplicationContext context;

    private Map<String,Object> implMap = new HashMap<>();

    Map<String, ActSpecialPerformService> actSpecialPerformServiceImplMap = null;

    public ActSpecialTaskService(){
        // 正常提交
        implMap.put("electronicSeal","fundElectronicSealsService");


    }

    /**
     * 获取其他的业务表单地址
     * @param taskVo 任务对象
     * @param activityImpl activityImpl对象
     */
    public void getFormPath(TaskVo taskVo, ActivityImpl activityImpl){
        String taskType = null;
        if ("true".equals(activityImpl.getProperty("electronicseal"))) {
            taskType = "electronicSeal";
        }

        // 调用实现类
        if (StringUtils.isNotBlank(taskType)){
            ActSpecialPerformService actSpecialPerformService = actSpecialPerformServiceImplMap.get(implMap.get(taskType));
            //actSpecialPerformService.createPerform(actId,taskId,dataId);
        }
    }

    /**
     * 检查流程是否到了某个特殊节点，如果是执行对应的方法
     * @param actId 流程实例ID
     */
    public void createPerform(String actId){

    }



    /**
     * 检查流程是否到了某个特殊节点，如果是执行对应的方法
     * @param actId 流程实例ID
     */
    @Transactional(readOnly = false)
    public void checkSpecialNode(String actId){
        if (actSpecialPerformServiceImplMap == null){
            actSpecialPerformServiceImplMap = context.getBeansOfType(ActSpecialPerformService.class);
        }

        Map<String, String> dataTableAndDataId = ActUtils.getDataTableAndDataId(actId);
        String dataId = dataTableAndDataId.get("actDataId");

        // 任务类型
        String taskType = null;
        String taskId = null;

        List<Task> taskList = taskService.createTaskQuery().processInstanceId(actId).list();

        //taskService.su

        for (Task task : taskList) {
            ActivityImpl activityImpl = ActUtils.getActivityImpl(task);
            // 电子签章签约节点
            if ("true".equals(activityImpl.getProperty("electronicseal"))){
                taskType = "electronicSeal";
                taskId = task.getId();
                break;
            }
        }

        // 调用实现类
        if (StringUtils.isNotBlank(taskType)){
            ActSpecialPerformService actSpecialPerformService = actSpecialPerformServiceImplMap.get(implMap.get(taskType));
            actSpecialPerformService.createPerform(actId,taskId,dataId);
        }


    }


}
